/*
	TimeLine 时间滑动插件 by　ＬｉＲｏｎｇ
	https://git.oschina.net/codediy/Time_Lime.git

*/
$(function(){
var _move=false;//移动标记  
var _x,_y;//鼠标离控件左上角的相对位置 
var w=825;
var step =16;
var poss ={};
var _domid=null; 
var myDate = new Date();

var bpos,epos,valpos;

var template ='<div class="time-line"><div class="cur-line" id="cur-line"></div></div><div class="poiner low" id="low"><div class="tag">--</div></div><div class="poiner high" id="high"><div class="tag">--</div></div><input type="hidden" name="low" id="low-val" value="0"><input type="hidden" name="high" id="high-val" value="0"><ul class="yearNum" id="yearNum"></ul>';

$('#time-wrap').append(template);
ToYear =myDate.getFullYear()-2;
FromYear = myDate.getFullYear()+1;

currMonth =myDate.getFullYear()+'-'+(myDate.getMonth()+1); 
beforeMonth=(myDate.getFullYear()-1) +'-'+myDate.getMonth();

createDateList(ToYear,FromYear);
function createDateList(start,end){
var html='';
for (var i = start; i <= end; i++) {
	html+='<li data-date="'+i+'" title="'+i+'" class="time_tag"></li>\n';
	for (var j = 1; j <= 12; j++) {
		html+='<li data-date="'+i+'-'+j+'" title="'+i+'-'+j+'"></li>\n';
	};					
}
$('#yearNum').append(html);	

}

$('#yearNum').find('li').each(function(index, el) {
 var _this =$(this);
 var num =_this.position().left;
 var timeNum =_this.attr('data-date');
 poss[num] =timeNum;

});

createTag(ToYear,FromYear);
function createTag(ToYear,FromYear) {

var num=[];
for (var i = ToYear; i <= FromYear; i++) {
	num.push(retpos(poss,i));
};
var html='';
for (var i =0;i < num.length; i++) {
		html +='<div class="year" style="left:'+num[i]+'px">'+(ToYear+i)+'</div>';
};
$('#yearNum').before(html);
}

function retpos (obj,num) {
if (typeof(obj) === 'object') {
	for(var i in obj){
		if (obj[i]==num) {
			return i;
		};						
	}
};
}

function initMonth(before,endtime){
$('#low .tag').html(before);
$('#high .tag').html(endtime);
$('#low-val').val(before);
$('#high-val').val(endtime);
bpos =retpos(poss,before);
epos =retpos(poss,endtime);
$('#low').css('left',bpos+'px');
$('#high').css('left',epos+'px');
}

initMonth(beforeMonth,currMonth);

$('#cur-line').css('left',$('#low').css('left'));
$('#cur-line').css('width',parseInt($('#high').css('left'))-parseInt($('#low').css('left')) +'px');

$("#low,#high").click(function(){  
//alert("click");//点击（松开后触发）
}).mousedown(function(e){  
    _move=true; 
    _domid =$(this); 
    _x=e.pageX-parseInt(_domid.css("left"));  
    _domid.fadeTo(20, 0.8);//点击后开始拖动并透明显示  
    _domid.css('z-index', '99');

	}); 
	 $(document).mousemove(function(e){  
        if(_move){
        	e.preventDefault();
     		var x=e.pageX-_x;	

        	if (parseInt(e.pageX-_x) >= w) {
        	 	x=w;
        	}
        	if (parseInt(e.pageX-_x) <=0) {
        	 	x =0;
        	};
        	x =Math.round(x/16)*16;
 
			var highLeft = parseInt($('#high').css('left'));
			var lowLeft = parseInt($('#low').css('left'));
			
			if (_domid.hasClass('low') && x>=highLeft || _domid.hasClass('high') && x<=lowLeft){
					return false;				 			
	 		}
			_domid.find('.tag').html(poss[x]);
	        if (_domid.hasClass('low')) {
	 			$('#cur-line').css('left',x==0?'0px':_domid.css("left"));
	 			$('#low-val').val(poss[x]);
	 			$('#high').css('z-index', '');
	 		};    

	        if (_domid.hasClass('high')) {
	 			$('#high-val').val(poss[x]);
	 			$('#low').css('z-index', '');
	 		};	

				$('#cur-line').css('width',parseInt($('#high').css('left'))-parseInt($('#low').css('left')) +'px');


            _domid.css({left:x});//控件新位置  

        }  
    }).mouseup(function(){  
    	if (_domid!=null) {
		    _move=false;  
		    _domid.fadeTo("fast", 1);//松开鼠标后停止移动并恢复成不透明  
//				_domid.find('.tag').html(poss[parseInt(_domid.css("left"))]);
//				_domid.find('.tag').html(possnum[valpos]);
	 
		};
  	}); 

$('#yearNum').find('li').click(function(event) {

		var time = $(this).attr('data-date');
		var pos = $(this).position().left;
		var low =$('#low').position().left;
		var lhw =parseInt($('#high').css('left'))-parseInt($('#low').css('left'));
		var midw =(lhw/2);

		if (pos < low+midw) {
			$('#low').css('left',pos);
			$('#cur-line').css('left',pos==0?'0px':pos);
			$('#low').find('.tag').html(time);
			$('#low-val').val(time);
		}else{
			$('#high').css('left',pos);
			$('#high').find('.tag').html(time);
			$('#high-val').val(time);
		}
			$('#cur-line').css('width',parseInt($('#high').css('left'))-parseInt($('#low').css('left')) +'px');
});
})
